using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Reflection;
using System.IO;
using T3K.SketchEditor.Core;
using T3K.SketchEditor.Configuration;

namespace T3K.SketchEditor.Forms
{
	static class Program
	{
		#region Fields

		public static MainForm MainForm = null;

		#endregion Fields


		#region Properties

		public static DocumentForm ActiveDocument
		{
			get
			{
				return MainForm.ActiveDocument;
			}
		}


		public static string Title
		{
			get
			{
				object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
				if (attributes.Length > 0)
				{
					AssemblyTitleAttribute titleAttribute = (AssemblyTitleAttribute)attributes[0];
					if (!String.IsNullOrEmpty(titleAttribute.Title))
						return titleAttribute.Title;
				}

				// If there was no Title attribute, or if the Title attribute was the empty string, return the .exe name
				return Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().CodeBase);
			}
		}

		#endregion Properties


		#region Methods

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main(string[] args)
		{

            string appPath = Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "SketchEditor");

            if (!Directory.Exists(appPath))
                Directory.CreateDirectory(appPath);

            CopyFolder("Config", Path.Combine(appPath, "Config"));
            CopyFolder("Images", Path.Combine(appPath, "Images"));
            CopyFolder("Templates", Path.Combine(appPath, "Templates"));
            CopyFolder("lib", Path.Combine(appPath, "lib"));
            File.Copy("avrbuilder.vars", Path.Combine(appPath, "avrbuilder.vars"), true);
            File.Copy("tags", Path.Combine(appPath, "tags"), true);

            System.IO.Directory.SetCurrentDirectory(appPath);

			Application.EnableVisualStyles();
			Application.SetCompatibleTextRenderingDefault(false);
			Application.Run(new MainForm(args));
		}

        static public void CopyFolder(string sourceFolder, string destFolder) {
            if (!Directory.Exists(destFolder))
                Directory.CreateDirectory(destFolder);
            string[] files = Directory.GetFiles(sourceFolder);
            foreach (string file in files) {
                string name = Path.GetFileName(file);
                string dest = Path.Combine(destFolder, name);
                if (name[0] != '.')
                    File.Copy(file, dest, true);
            }
            string[] folders = Directory.GetDirectories(sourceFolder);
            foreach (string folder in folders) {
                string name = Path.GetFileName(folder);
                string dest = Path.Combine(destFolder, name);
                if (name[0] != '.')
                    CopyFolder(folder, dest);
            }
        }

		#endregion Methods
	}
}